﻿using csLTDMC;
//using Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MC
{
    public class MCLeisai
    {
        const bool bJL=true;
        private ushort cardID = 255;
        private int[] curPos = new int[8];
        public bool Init()
        {
            if (LTDMC.dmc_board_init() <= 0)
                return false;
            ushort My_CardNum=8;      //定义卡数
            ushort[] My_CardList = new ushort[8];   //定义卡号数组
            uint[] My_CardTypeList = new uint[8];   //定义各卡类型
            LTDMC.dmc_get_CardInfList(ref My_CardNum, My_CardTypeList , My_CardList);
            cardID = My_CardList[0];
            LTDMC.dmc_set_pulse_outmode(cardID, 0, 0);    //0 脉冲高，方向高
            LTDMC.dmc_set_pulse_outmode(cardID, 1, 0);
            LTDMC.dmc_set_pulse_outmode(cardID, 2, 0);

            LTDMC.dmc_write_sevon_pin(cardID, 0, 0); //0 低电平启用
            LTDMC.dmc_write_sevon_pin(cardID, 1, 0); //0 低电平启用
            LTDMC.dmc_write_sevon_pin(cardID, 2, 0); //0 低电平启用


            if (bJL)
            {
                LTDMC.dmc_set_alm_mode(cardID, 0, 1, 1, 0);//报警参数为0=低电平有效
                LTDMC.dmc_set_alm_mode(cardID, 1, 1, 1, 0);
                LTDMC.dmc_set_alm_mode(cardID, 2, 1, 1, 0);

                LTDMC.dmc_set_home_pin_logic(cardID, 0, 0, 0);    //设置原点为高电平有效
                LTDMC.dmc_set_home_pin_logic(cardID, 1, 0, 0);
                LTDMC.dmc_set_home_pin_logic(cardID, 2, 0, 0);

                LTDMC.dmc_set_el_mode(cardID, 0, 1, 1, 0);//设置限位为低电平有效
                LTDMC.dmc_set_el_mode(cardID, 1, 1, 1, 0);
                LTDMC.dmc_set_el_mode(cardID, 2, 1, 1, 0);
            }
            else
            {
                LTDMC.dmc_set_alm_mode(cardID, 0, 1, 0, 0);//报警参数为0=低电平有效
                LTDMC.dmc_set_alm_mode(cardID, 1, 1, 0, 0);
                LTDMC.dmc_set_alm_mode(cardID, 2, 1, 0, 0);

                LTDMC.dmc_set_home_pin_logic(cardID, 0, 0, 0);    //设置原点为高电平有效
                LTDMC.dmc_set_home_pin_logic(cardID, 1, 0, 0);
                LTDMC.dmc_set_home_pin_logic(cardID, 2, 0, 0);

                LTDMC.dmc_set_el_mode(cardID, 0, 1, 0, 0);//设置限位为低电平有效
                LTDMC.dmc_set_el_mode(cardID, 1, 1, 0, 0);
                LTDMC.dmc_set_el_mode(cardID, 2, 1, 0, 0);
            }
            return true;
        }

        public void SetPosition(ushort axis, int curPos)
        {
            LTDMC.dmc_set_position(cardID, axis, curPos);
        }

        public void Home(ushort axis, int minVel, int maxVel, double tAcc, ushort dir = 1)
        {
            ushort MyCardNo, Myorg_logic, Myfilter, /*Myhome_dir,*/ Mymode, MyEZ_count;
            double Myvel_mode;
            MyCardNo = 0; //卡号
            Myorg_logic = 0; //原点信号有效电平为低电平有效
            Myfilter = 0; //保留参数
                          //Myhome_dir = 1; //回零方向为正向
            Myvel_mode = 1; //回零模式为低速
            Mymode = 0; //回零方式为一次回零
            MyEZ_count = 0; //保留参数


            LTDMC.dmc_set_pulse_outmode(cardID, axis, 0);  //设置脉冲输出模式
            LTDMC.dmc_set_profile(cardID, axis, minVel, maxVel, tAcc, tAcc, 500);//设置速度曲线
            LTDMC.dmc_set_homemode(cardID, axis, dir, Myvel_mode, Mymode, 0);//设置回零方式
            LTDMC.dmc_home_move(cardID, axis);//回零动作
        }

        public void MoveAbs(ushort axis, int pos, double minVel, double maxVel, double tAcc)
        {
            double MyStop_Vel = 200; //停止速度 200pulse/s
            LTDMC.dmc_set_profile(cardID, axis, minVel, maxVel, tAcc, tAcc, MyStop_Vel);//设置单轴运动速度曲线	
            LTDMC.dmc_pmove(cardID, axis, pos, 1);//执行点位运动
            if (cardID == 255)
            {
                curPos[axis] = pos;
            }
        }

        public bool IsMoveDone(ushort axis)
        {
//#if DEBUGOffLINE ==1
//            return true;
//#endif
            return cardID==255 || LTDMC.dmc_check_done(cardID, axis)==1;
        }

        public void WaitMoveDone(ushort axis)
        {
            while (IsMoveDone(axis) == false)
            {
                Thread.Sleep(10);
            }
        }

        public void WaitMoveDone(ushort[] axiss)
        {
            while (true)
            {
                bool res = true;
                for (int i = 0; i < axiss.Length; i++)
                {
                    res &= IsMoveDone(axiss[i]);
                }
                if (res)
                    break;
                Thread.Sleep(10);
            }
        }

        public void Stop(ushort axis)
        {
            LTDMC.dmc_stop(cardID, axis, 0);
        }

        public void EStop(ushort axis)
        {
            LTDMC.dmc_stop(cardID, axis, 1);
        }

        public int GetCurPos(ushort axis)
        {
            if (cardID == 255)
                return curPos[axis];
            else
                return LTDMC.dmc_get_position(cardID, axis);
        }

        public bool GetDI(ushort bit)
        {
            return LTDMC.dmc_read_inbit(cardID, bit)==0;
        }

        public uint GetGI()
        {
            uint w = LTDMC.dmc_read_inport(cardID, 0);
            w = ~w;
            return w;
        }

        public void SetDO(ushort bit, ushort on_off)
        {
            LTDMC.dmc_write_outbit(cardID, bit, on_off);
        }
    }
}
